//Project 3 - CS 3451 - Labyrinth Lite
//Instructor: Jarek Rossignac
//Author: Tyler Powell
//Date: September 2011

//Image Load
PImage myFace;

void setupFace() { //load my pic into memory
  myFace = loadImage("data/face.jpg");
}

//Point Information

ArrayList<pt> labPoints = new ArrayList<pt>();
ArrayList<vec> walls = new ArrayList<vec>();

boolean align = false;
void addPoint(float x, float y, boolean align) { //add a point to the list of points
  if(align && !labPoints.isEmpty()) {
    pt p = alignPoint(x,y,labPoints.get(labPoints.size()-1)); //align the point before adding it to the list
    if(wallValid(p)) { //if the wall does not cross another wall
      labPoints.add(p);
      if (labPoints.size() >= 2) {
        pt previous = labPoints.get(labPoints.size()-2);
        walls.add(new vec(previous,p));
      }
    }
  } else {
    pt p = new pt(x,y);
    if(labPoints.isEmpty()) {
      labPoints.add(p);
    }
    else if(wallValid(p)) {
      labPoints.add(p);
      if (labPoints.size() >= 2) {
        pt previous = labPoints.get(labPoints.size()-2);
        walls.add(new vec(previous,p));
      }
    }
  }
}

void addPoint(pt p) {
  addPoint(p.x,p.y,align);
}

pt alignPoint(float x, float y, pt p) { //align a point with the x or y axis
  if(abs(p.x-x) > abs(p.y-y)) {
    return new pt(x,p.y);
  } else {
    return new pt(p.x,y);
  }
}

void saveData() { //save the points to a file
  String file = ("data/points.pts");
  String[] data = new String[labPoints.size()+1];
  for(int i = 0; i < labPoints.size(); i++) {
    pt p = labPoints.get(i);
    data[i] = p.x + " " + p.y;
  }
  data[labPoints.size()]=ballLocation.x + " " + ballLocation.y;
  saveStrings(file,data);
}

void loadData() { //load the points from a file
  String file = ("data/points.pts");
  labPoints = new ArrayList<pt>();
  String[] data = loadStrings(file);
  int dataSize = data.length;
  for(int i = 0; i < dataSize-1; i++) {
    String[] dataLine = data[i].split(" ");
    labPoints.add(new pt(float(dataLine[0]),float(dataLine[1])));
  }
  String[] ballData = data[dataSize-1].split(" ");
  placeBall(float(ballData[0]),float(ballData[1]));
}

int pictureCounter = 1;
void takePicture() {
  saveFrame("data/screenshot"+nf(pictureCounter++,3)+".jpg");
}
